/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.kernel;

import org.openaecfm.model.kernel.PropertySetDefinition;
import org.openaecfm.model.kernel.Root;
import org.openaecfm.model.kernel.TypeObject;
import org.openaecfm.service.kernel.ITypeObjectManager;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class TypeObjectIO implements
		IEntityInstanceUtilInput<TypeObject>,
		IEntityInstanceUtilOutput<TypeObject> {
	private IEntityInstanceUtilInput<Root> utilInput;
	private IEntityInstanceUtilOutput<Root> utilOutput;
	private ITypeObjectManager manager;

	public void setManager(ITypeObjectManager manager) {
		this.manager = manager;
	}

	public void setUtilOutput(IEntityInstanceUtilOutput<Root> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<Root> utilInput) {
		this.utilInput = utilInput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(TypeObject pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set ApplicableOccurrence
		if (pojo.getApplicableOccurrence() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(
					pojo.getApplicableOccurrence(), StringParameter.UTF_8));
		}

		// Set HasPropertySets
		if (pojo.getHasPropertySets().isEmpty()) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (PropertySetDefinition psd : pojo.getHasPropertySets()) {
				EntityInstanceNameParameter n = new EntityInstanceNameParameter(
						appDataManager.getName(psd));
				l.add(n);
			}
			sei.addParameter(l);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei, TypeObject pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		utilInput.resolveNotReferenceParameter(ei, pojo);

		// Set ApplicableOccurrence
		Parameter appocc = sei.getParameter(4);
		if (appocc.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) appocc;
			pojo.setApplicableOccurrence(s.toUTF8());
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, TypeObject pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set HasPropertySets
		Parameter hasp = sei.getParameter(5);
		if (hasp.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) hasp;
			for (Parameter p : l) {
				EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
				manager.addHasPropertySet(pojo,
						(PropertySetDefinition) appDataManager.getObject(n
								.toInstanceName()));
			}
		}
	}
}
